package com.htt.bms.security;

import com.htt.bms.common.exception.BusinessException;
import com.htt.bms.domain.sys.SysUser;
import com.htt.bms.service.system.SysUserService;
import com.htt.bms.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 侯婷婷
 * @version 1.0
 * @description 用户认证serviceImpl
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsManager, UserDetailsPasswordService {

    @Resource
    private SysUserService sysUserService;

    /**
     * 通过用户名加载用户
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名获取用户
        SysUser user = sysUserService.login(username);
        //获取用户为空
        if(Objects.isNull(user)){
            throw new BusinessException("用户名获取密码有误！");
        }

        return new SecurityUtils(user);
    }

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        return null;
    }

    @Override
    public void createUser(UserDetails userDetails) {

    }

    @Override
    public void updateUser(UserDetails userDetails) {

    }

    @Override
    public void deleteUser(String s) {

    }

    @Override
    public void changePassword(String s, String s1) {

    }

    @Override
    public boolean userExists(String s) {
        return false;
    }
}
